//实现Stock类
#include<iostream>
#include"stock20.h"
Stock::Stock()
{
    using std::cout;
    cout<<"调用默认构造函数\n";
    company="no name";
    shares=0;
    share_val=0;
    set_tot();
}
Stock::Stock(const std::string &co,long n,double pr)
{
    using std::cout;
    company=co;
    if (n<0)
    {
        cout<<"number of shares can't be negative;"
                <<company<<"shares set to 0.\n";
                shares=0;
    }
    else
    {
        shares=n;
    }
    share_val=pr;
    set_tot();
}
Stock::~Stock()
{
    using std::cout;
    cout<<"再见："<<company<<"!\n";
}
void Stock::buy(long num,double price)
{
    using std::cout;
    if (num<0)
    {
        cout<<"number of shares purchased can't be negative."
        <<"transaction is aborted.\n";
    }
    else
    {
        shares+=num;
        share_val=price;
        set_tot();
    }
}
void Stock::sell(long num,double price)
{
    using std::cout;
    if (num<0)
    {
        cout<<"number of shares sold can't be negative."
        <<"transaction is aborted.\n";
    }
    else if (num>shares)
    {
        cout<<"you can't sell more than you have."
        <<"transaction is aborted.\n";
    }
    else
    {
        shares-=num;
        share_val=price;
        set_tot();
    }
}
void Stock::update(double price)
{
    share_val=price;
    set_tot();
}
void Stock::show()
{
    using std::cout;
    cout<<"company: "<<company
    <<"shares:"<<shares<<'\n'
    <<"share price:"<<share_val
    <<"total worth:"<<total_val<<'\n';
}
const Stock & Stock::topval(const Stock & s) const
{
    if (s.total_val>total_val)
    {
        return s;
    }
    else
    {
        return *this;
    }
    
}